package com.nic.view.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nic.view.R;
import com.nic.view.util.Utility;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 语音录制
 * RecordBean
 */
public class RecordAdapter extends BaseAdapter {

    private Context context;
    private List<String> voicePathList = new ArrayList<>();
    private List<String> timeList = new ArrayList<>();

    public RecordAdapter(Context context) {
        this.context = context;
    }

    /**
     * 添加数据
     *
     * @param mLists 集合数据
     */
    public void setList(List<String> mLists) {
        if (mLists == null || mLists.isEmpty()) {
            return;
        }
        voicePathList.addAll(mLists);
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     */
    public void add(String path, String duration) {
        if (!TextUtils.isEmpty(path)) {
            voicePathList.add(path);
            timeList.add(duration);
            notifyDataSetChanged();
        }

    }

    public List<String> getVoicePathList() {
        return voicePathList;
    }

    @Override
    public int getCount() {
        return voicePathList.size();
    }

    @Override
    public Object getItem(int position) {
        return voicePathList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 单条播放变量
     * true有录音正在播放
     * false没有播放的录音
     */
    private boolean isplay = false;
    private MediaPlayer myPlayer;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_record, null);
            holder.iv_play = (ImageView) convertView.findViewById(R.id.iv_play);//播放按钮
            holder.tv_duration = (TextView) convertView.findViewById(R.id.tv_duration);//时长
            holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);//删除
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        final String path = voicePathList.get(position);
        if (!timeList.isEmpty() && timeList.size() > position) {
            String duration = timeList.get(position);
            holder.tv_duration.setText(duration);
        }

        /**
         * 播放按钮点击事件
         */
        holder.iv_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isplay) {
                    return;
                }
                try {
                    if (Utility.isFolderExist(path)) {
                        myPlayer = new MediaPlayer();
                        myPlayer.setDataSource(path);// 设置数据源
                        myPlayer.prepare();// 准备
                        myPlayer.start(); // 开始播放
                        isplay = true;
                        holder.iv_play.setImageResource(R.drawable.voice_play_anim);
                        AnimationDrawable animationDrawable = (AnimationDrawable) holder.iv_play.getDrawable();
                        //开始动画
                        animationDrawable.start();
                        // 播放完毕回调方法
                        myPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                isplay = false;
                                myPlayer = null;
                                holder.iv_play.setImageResource(R.drawable.ic_play_record3);
                            }
                        });
                    } else {
                        Toast.makeText(context, "播放失败！", Toast.LENGTH_SHORT).show();
                        isplay = false;
                        myPlayer = null;
                        holder.iv_play.setImageResource(R.drawable.ic_play_record3);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "播放失败！", Toast.LENGTH_SHORT).show();
                    isplay = false;
                    myPlayer = null;
                    holder.iv_play.setImageResource(R.drawable.ic_play_record3);
                }
            }
        });
        /**
         * 删除按钮点击事件
         */
        holder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isplay) {
                    return;
                }
                try {

                    if (Utility.isFolderExist(path)) {
                        Utility.delete(new File(path));
                    }
                    voicePathList.remove(position);
                    if (timeList.size() > position) {
                        timeList.remove(position);
                    }
                    notifyDataSetChanged();
                    if (voicePathList.contains(path)) {
                        voicePathList.remove(path);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        // 返回视图
        return convertView;
    }

    /**
     * 定义一个holder类
     */
    class Holder {
        ImageView iv_play;//播放
        TextView tv_duration;//时长
        ImageView iv_delete;//删除
    }

    /**
     * 获取播放器
     *
     * @return 播放器
     */
    public MediaPlayer getMediaPlayer() {
        return myPlayer;
    }
}
